#include "2048.h"
int count = 1;
int comper = 0;
void DisplayBoard(int board[ROW][COL], int row, int col)  //初始化棋盘
{
    if (comper > 0)
    {
        _randow(board);
    }
    while (count != 0)
    {
        _randow(board);
        _randow(board);
        count = count - 1;
    }
    int i = 0;
    for (i; i < row; i++)
    {
        int j = 0;
        for (j = 0; j < col; j++)
        {
            printf(" +-------+");
        }
        printf("\n");
        if (i < row)
        {
            for (j = 0; j < col; j++)
            {

                printf(" |");
                if (j < col)
                {
                    if (board[i][j] != 0)
                    {
                        printf("   %-4d|", board[i][j]);
                    }
                    else
                        printf("   %c   |", ' ');
                }
            }
            printf("\n");
        }
        for (j = 0; j < col; j++)
        {
            printf(" +-------+");
        }
        printf("\n");
    }
    comper = 0;
}
void _randow(int (*board)[4])//产生随机0-3的随机数
{
    while (1)
    {
        int math[5] = {2, 2, 4, 2, 2};
        int number = rand() % 4 + 1;
        int a = rand() % 4;
        int b = rand() % 4;
        if (board[a][b] == 0)
        {
            board[a][b] = math[number];
            break;
        }
    }
}
void move(int board[ROW][COL], int row, int col) // 使棋盘移动并且合并
{

    char input;
    while (failure(board, ROW, COL) != 0 && win(board, ROW, COL != 0))
    {
        failure(board, ROW, COL);
        win(board, ROW, COL);
        int a[4][4] = {0};
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                a[i][j] = board[i][j];
            }
        }
        printf("请选择移动方向:上(w)下(s)左(a)右(d)\n");
        printf("——> ");
        scanf("%c", &input);
        while (getchar() != '\n')
            ;
        printf("\n");
        switch (input)
        {
        case 'w':
        {
            add_w(board);
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    if (a[i][j] != board[i][j])
                        comper++;
                }
            }
            system("clear");
            DisplayBoard(board, ROW, COL);
            break;
        }
        case 's':
        {
            add_s(board);
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    if (a[i][j] != board[i][j])
                        comper++;
                }
            }
            system("clear");
            DisplayBoard(board, ROW, COL);
            break;
        }
        case 'a':
        {
            add_a(board);
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    if (a[i][j] != board[i][j])
                        comper++;
                }
            }
            system("clear");
            DisplayBoard(board, ROW, COL);
            break;
        }
        case 'd':
        {
            add_d(board);
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    if (a[i][j] != board[i][j])
                        comper++;
                }
            }
            system("clear");
            DisplayBoard(board, ROW, COL);
            break;
        }
        default:
        {
            printf("输入错误，请重新输入\n");
            break;
        }
        }
    }
}
void add_w(int (*board)[4]) // 实现向上移动并且合并
{
    int row = 0, col = 0;
    for (col; col < 4; col++)
    {
        int p[4] = {0};
        int ad = 0;
        for (row = 0; row < 4; row++)
        {
            if (board[row][col] != 0)
            {
                p[ad] = board[row][col]; // 记录数值
                ad++;                    // 记录个数
            }
        }
        if (ad == 1)
        {
            board[0][col] = p[0];
            board[1][col] = 0;
            board[2][col] = 0;
            board[3][col] = 0;
        }
        if (ad == 2)
        {
            if (p[0] != p[1])
            {
                board[0][col] = p[0];
                board[1][col] = p[1];
                board[2][col] = 0;
                board[3][col] = 0;
            }
            else
            {
                board[0][col] = p[0] + p[1];
                board[1][col] = 0;
                board[2][col] = 0;
                board[3][col] = 0;
            }
        }
        if (ad == 3)
        {
            if (p[0] != p[1])
            {
                if (p[1] != p[2])
                {
                    board[0][col] = p[0];
                    board[1][col] = p[1];
                    board[2][col] = p[2];
                    board[3][col] = 0;
                }
                else
                {
                    board[0][col] = p[0];
                    board[1][col] = p[1] + p[2];
                    board[2][col] = 0;
                    board[3][col] = 0;
                }
            }
            else
            {
                board[0][col] = p[0] + p[1];
                board[1][col] = p[2];
                board[2][col] = 0;
                board[3][col] = 0;
            }
        }
        if (ad == 4)
        {
            if (p[0] != p[1])
            {
                if (p[1] != p[2])
                {
                    if (p[2] != p[3])
                    {
                        board[0][col] = p[0];
                        board[1][col] = p[1];
                        board[2][col] = p[2];
                        board[3][col] = p[3];
                    }
                }
            }
            if (p[0] == p[1])
            {
                if (p[2] == p[3])
                {
                    board[0][col] = p[0] + p[1];
                    board[1][col] = p[2] + p[3];
                    board[2][col] = 0;
                    board[3][col] = 0;
                }
            }
            if (p[0] != p[1])
            {
                if (p[1] == p[2])
                {
                    if (p[2] != p[3])
                    {
                        board[0][col] = p[0];
                        board[1][col] = p[1] + p[2];
                        board[2][col] = p[3];
                        board[3][col] = 0;
                    }
                }
            }
            if (p[0] == p[1])
            {
                if (p[2] != p[3])
                {
                    board[0][col] = p[0] + p[1];
                    board[1][col] = p[2];
                    board[2][col] = p[3];
                    board[3][col] = 0;
                }
            }
            if (p[0] != p[1])
            {
                if (p[1] != p[2])
                {
                    if (p[2] == p[3])
                    {
                        board[0][col] = p[0];
                        board[1][col] = p[1];
                        board[2][col] = p[2] + p[3];
                        board[3][col] = 0;
                    }
                }
            }
        }
    }
}
void add_s(int (*board)[4])//实现向下移动并且合并
{
    int row = 0, col = 0;
    for (col; col < 4; col++)
    {
        int p[4] = {0};
        int ad = 0;
        for (row = 0; row < 4; row++)
        {
            if (board[row][col] != 0)
            {
                p[ad] = board[row][col]; // 记录数值
                ad++;                    // 记录个数
            }
        }
        if (ad == 1)
        {
            board[0][col] = 0;
            board[1][col] = 0;
            board[2][col] = 0;
            board[3][col] = p[0];
        }
        if (ad == 2)
        {
            if (p[1] != p[0])
            {
                board[0][col] = 0;
                board[1][col] = 0;
                board[2][col] = p[0];
                board[3][col] = p[1];
            }
            else
            {
                board[0][col] = 0;
                board[1][col] = 0;
                board[2][col] = 0;
                board[3][col] = p[0] + p[1];
            }
        }
        if (ad == 3)
        {
            if (p[2] != p[1])
            {
                if (p[1] != p[0])
                {
                    board[0][col] = 0;
                    board[1][col] = p[0];
                    board[2][col] = p[1];
                    board[3][col] = p[2];
                }
                else
                {
                    board[0][col] = 0;
                    board[1][col] = 0;
                    board[2][col] = p[0] + p[1];
                    board[3][col] = p[2];
                }
            }
            else
            {
                board[0][col] = 0;
                board[1][col] = 0;
                board[2][col] = p[0];
                board[3][col] = p[2] + p[1];
            }
        }
        if (ad == 4)
        {
            if (p[3] != p[2])
            {
                if (p[2] != p[1])
                {
                    if (p[1] != p[0])
                    {
                        board[0][col] = p[0];
                        board[1][col] = p[1];
                        board[2][col] = p[2];
                        board[3][col] = p[3];
                    }
                }
            }
            if (p[3] == p[2])
            {
                if (p[1] == p[0])
                {
                    board[0][col] = 0;
                    board[1][col] = 0;
                    board[2][col] = p[0] + p[1];
                    board[3][col] = p[3] + p[2];
                }
            }
            if (p[3] != p[2])
            {
                if (p[2] == p[1])
                {

                    board[0][col] = 0;
                    board[1][col] = p[0];
                    board[2][col] = p[1] + p[2];
                    board[3][col] = p[3];
                }
            }
            if (p[3] != p[2])
            {
                if (p[2] != p[1])
                {
                    if (p[1] == p[0])
                    {
                        board[0][col] = 0;
                        board[1][col] = p[0] + p[1];
                        board[2][col] = p[2];
                        board[3][col] = p[3];
                    }
                }
            }
            if (p[3] == p[2])
            {
                if (p[1] != p[0])
                {
                    board[0][col] = 0;
                    board[1][col] = p[0];
                    board[2][col] = p[1];
                    board[3][col] = p[2] + p[3];
                }
            }
        }
    }
}
void add_a(int (*board)[4])//实现向左移动并且合并
{
    int row = 0, col = 0;
    for (col; col < 4; col++)
    {
        int p[4] = {0};
        int ad = 0;
        for (row = 0; row < 4; row++)
        {
            if (board[col][row] != 0)
            {
                p[ad] = board[col][row]; // 记录数值
                ad++;                    // 记录个数
            }
        }
        if (ad == 1)
        {
            board[col][0] = p[0];
            board[col][1] = 0;
            board[col][2] = 0;
            board[col][3] = 0;
        }
        if (ad == 2)
        {
            if (p[0] != p[1])
            {
                board[col][0] = p[0];
                board[col][1] = p[1];
                board[col][2] = 0;
                board[col][3] = 0;
            }
            else
            {
                board[col][0] = p[0] + p[1];
                board[col][1] = 0;
                board[col][2] = 0;
                board[col][3] = 0;
            }
        }
        if (ad == 3)
        {
            if (p[0] != p[1])
            {
                if (p[1] != p[2])
                {
                    board[col][0] = p[0];
                    board[col][1] = p[1];
                    board[col][2] = p[2];
                    board[col][3] = 0;
                }
                else
                {
                    board[col][0] = p[0];
                    board[col][1] = p[1] + p[2];
                    board[col][2] = 0;
                    board[col][3] = 0;
                }
            }
            else
            {
                board[col][0] = p[0] + p[1];
                board[col][1] = p[2];
                board[col][2] = 0;
                board[col][3] = 0;
            }
        }
        if (ad == 4)
        {
            if (p[0] != p[1])
            {
                if (p[1] != p[2])
                {
                    if (p[2] != p[3])
                    {
                        board[col][0] = p[0];
                        board[col][1] = p[1];
                        board[col][2] = p[2];
                        board[col][3] = p[3];
                    }
                }
            }
            if (p[0] == p[1])
            {
                if (p[2] == p[3])
                {
                    board[col][0] = p[0] + p[1];
                    board[col][1] = p[2] + p[3];
                    board[col][2] = 0;
                    board[col][3] = 0;
                }
            }
            if (p[0] != p[1])
            {
                if (p[1] == p[2])
                {
                    if (p[2] != p[3])
                    {
                        board[col][0] = p[0];
                        board[col][1] = p[1] + p[2];
                        board[col][2] = p[3];
                        board[col][3] = 0;
                    }
                }
            }
            if (p[0] == p[1])
            {
                if (p[2] != p[3])
                {
                    board[col][0] = p[0] + p[1];
                    board[col][1] = p[2];
                    board[col][2] = p[3];
                    board[col][3] = 0;
                }
            }
            if (p[0] != p[1])
            {
                if (p[1] != p[2])
                {
                    if (p[2] == p[3])
                    {
                        board[col][0] = p[0];
                        board[col][1] = p[1];
                        board[col][2] = p[2] + p[3];
                        board[col][3] = 0;
                    }
                }
            }
        }
    }
}
void add_d(int (*board)[4])//实现向右移动并且合并
{
    int row = 0, col = 0;
    for (col; col < 4; col++)
    {
        int p[4] = {0};
        int ad = 0;
        for (row = 0; row < 4; row++)
        {
            if (board[col][row] != 0)
            {
                p[ad] = board[col][row]; // 记录数值
                ad++;                    // 记录个数
            }
        }
        if (ad == 1)
        {
            board[col][0] = 0;
            board[col][1] = 0;
            board[col][2] = 0;
            board[col][3] = p[0];
        }
        if (ad == 2)
        {
            if (p[1] != p[0])
            {
                board[col][0] = 0;
                board[col][1] = 0;
                board[col][2] = p[0];
                board[col][3] = p[1];
            }
            else
            {
                board[col][0] = 0;
                board[col][1] = 0;
                board[col][2] = 0;
                board[col][3] = p[0] + p[1];
            }
        }
        if (ad == 3)
        {
            if (p[2] != p[1])
            {
                if (p[1] != p[0])
                {
                    board[col][0] = 0;
                    board[col][1] = p[0];
                    board[col][2] = p[1];
                    board[col][3] = p[2];
                }
                else
                {
                    board[col][0] = 0;
                    board[col][1] = 0;
                    board[col][2] = p[0] + p[1];
                    board[col][3] = p[2];
                }
            }
            else
            {
                board[col][0] = 0;
                board[col][1] = 0;
                board[col][2] = p[0];
                board[col][3] = p[2] + p[1];
            }
        }
        if (ad == 4)
        {
            if (p[3] != p[2])
            {
                if (p[2] != p[1])
                {
                    if (p[1] != p[0])
                    {
                        board[col][0] = p[0];
                        board[col][1] = p[1];
                        board[col][2] = p[2];
                        board[col][3] = p[3];
                    }
                }
            }
            if (p[3] == p[2])
            {
                if (p[1] == p[0])
                {
                    board[col][0] = 0;
                    board[col][1] = 0;
                    board[col][2] = p[0] + p[1];
                    board[col][3] = p[3] + p[2];
                }
            }
            if (p[3] != p[2])
            {
                if (p[2] == p[1])
                {

                    board[col][0] = 0;
                    board[col][1] = p[0];
                    board[col][2] = p[1] + p[2];
                    board[col][3] = p[3];
                }
            }
            if (p[3] != p[2])
            {
                if (p[2] != p[1])
                {
                    if (p[1] == p[0])
                    {
                        board[col][0] = 0;
                        board[col][1] = p[0] + p[1];
                        board[col][2] = p[2];
                        board[col][3] = p[3];
                    }
                }
            }
            if (p[3] == p[2])
            {
                if (p[1] != p[0])
                {
                    board[col][0] = 0;
                    board[col][1] = p[0];
                    board[col][2] = p[1];
                    board[col][3] = p[2] + p[3];
                }
            }
        }
    }
}
int win(int board[ROW][COL], int row, int col)// 判断是否胜利的条件
{
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            if (board[i][j] == 2048)
            {
                printf("行啊小子，这都被你赢了！\n");
                return 0;
            }
        }
    }
    return 1;
}
int failure(int board[ROW][COL], int row, int col)  //判断是否失败的条件
{
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            if (board[i][j] == 0)
            {
                return 1;
            }
        }
    }
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col - 1; j++)
        {
            if (board[i][j] == board[i][j + 1] || board[i][j] == board[i + 1][j])
            {
                return 1;
            }
        }
    }
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            if (board[i][j] == 2048)
            {
                return 1;
            }
        }
    }
    printf("哈哈哈哈，你也太菜了吧！这么快就输啦？\n");
    return 0;
}